<?php

namespace App\Http\Controllers\Admin;

use App\Models\Fangattr;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class FangAttrController extends Controller
{
    /**
     * 列表显示
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        $model = new Fangattr();
        $data  = $model->getAllList();
        return view('admin.fangattr.index', compact('data'));
    }

    /**
     * 添加页面显示
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function create()
    {
        $data = (new Fangattr())->getAllList();
        return view('admin.fangattr.create', compact('data'));
    }

    public function upimg(Request $request){
        $pic = config('uploadpic.img');
        if ($request->hasFile('file')) {
            $info = $request->file('file')->store('', 'fangattr');
            $pic  = '/uploads/fangattr/images/' . $info;
        }
        return ['code' => 200, 'src' => $pic];
    }

    /**
     * 添加--保存至数据库
     * @param Request $request
     * @return array
     */
    public function store(Request $request)
    {
        try {
            $data = $this->validate($request, [
                'name'       => 'required|unique:fangattrs,name',
                'icon'       => 'required',
                'field_name' => 'nullable',
                'pid'        => 'required'
            ]);
        } catch (\Exception $e) {
            return ['msg' => '验证不通过', 'status' => 404];
        }
        Fangattr::create($data);
        return ['msg' => '添加节点成功', 'status' => 200];
    }

    /**
     * Display the specified resource.
     *
     * @param \App\Models\Fangattr $fangattr
     * @return \Illuminate\Http\Response
     */
    public function show(Fangattr $fangattr)
    {
        //
    }

    /**
     * @param Fangattr $fangattr
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit(Fangattr $fangattr)
    {
        $data = (new Fangattr())->getAllList();
        return view('admin.fangattr.edit', compact('data', 'fangattr'));
    }

    /**
     * @param Request $request
     * @param Fangattr $fangattr
     * @return array
     */
    public function update(Request $request, Fangattr $fangattr)
    {
        try {
            $data = $this->validate($request, [
                'name'       => 'required',
                'icon'       => 'required',
                'pid'        => 'required',
                'field_name' => 'nullable'
            ]);
        } catch (\Exception $e) {
            return ['msg' => $e->getMessage(), 'status' => 404];
        }
        $fangattr->update($data);
        return ['msg' => '修改成功', 'status' => 200];
    }

    /**
     * @param Fangattr $fangattr
     * @return string[]
     * @throws \Exception
     */
    public function destroy(Fangattr $fangattr)
    {
        $fangattr->delete();
        return ['msg' => '删除成功', 'status' => 200];
    }
}
